package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.digest.MD5;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.ts.api.http.org.entity.apply.OrgApplyXinPuHui;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.HashMap;
import java.util.Map;

/**
 *  安心分期-全广东公积金30
 *
 *  @author 吴佳宝
 */
@ApiPlan(productId = "344", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5XinPuHui implements ApiOrgPhoneMd5ConvertHandler {

    public static final String APP_KEY = "28e984cb215deff6";
    public static final String APP_SECRET = "8ef010fa995bfa91fd152d2f25baa6c5";

    @Override
    public Object dataExchange(ApiDataExchangeParameter ApiDataChangeMessage) throws Exception {
        String time = String.valueOf(System.currentTimeMillis() / 1000);
        String phoneMd5 = ApiDataChangeMessage.getPhoneMd5();
        String signStr = APP_KEY + APP_SECRET + phoneMd5 + time;
        String s = DigestUtils.md5Hex(signStr);

        Map<Object, Object> newMap = new HashMap<>();
        newMap.put("appKey", APP_KEY);
        newMap.put("sign", s);
        newMap.put("timestamp", time);
        newMap.put("phoneMd5", phoneMd5);

        return newMap;
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyMessage) throws Exception {
        String time = String.valueOf(System.currentTimeMillis() / 1000);
        OrgApplyXinPuHui data = new OrgApplyXinPuHui();
        data.setAppKey(APP_KEY);
        data.setTimestamp(time);
        // 手机号明文
        data.setPhone(apiApplyMessage.getPhone());
        data.setPhoneMd5(MD5.create().digestToHex(apiApplyMessage.getPhone()));
        // 用户名
        data.setRealName(apiApplyMessage.getUserName());
        // 城市
        data.setProvince(apiApplyMessage.getProvinceName());
        data.setCity(apiApplyMessage.getCity());
        data.setIdCard("无");
        // 年龄
        data.setAge(Integer.valueOf(apiApplyMessage.getAge()));
        // 性别
        Integer sex = ChooseEq.create(apiApplyMessage.getSex())
                .when("0", 1)
                .when("1", 0)
                .end(1);
        data.setGender(sex);
        // 月收入
        String monthlySalary = ChooseEq.create()
                .when(apiApplyMessage.getMonthlySalary(), "1", "3000")
                .when(apiApplyMessage.getMonthlySalary(), "2", "8000")
                .when(apiApplyMessage.getMonthlySalary(), "3", "15000")
                .when(apiApplyMessage.getMonthlySalary(), "4", "30000")
                .when(apiApplyMessage.getMonthlySalary(), "5", "50000")
                .end("0");
        data.setWageMonth(monthlySalary);
        // 学历
        Integer educationAttainment = ChooseEq.create()
                .when(apiApplyMessage.getEducationAttainment(), "1", 1)
                .when(apiApplyMessage.getEducationAttainment(), "2", 3)
                .when(apiApplyMessage.getEducationAttainment(), "3", 2)
                .when(apiApplyMessage.getEducationAttainment(), "4", 4)
                .when(apiApplyMessage.getEducationAttainment(), "5", 5)
                .when(apiApplyMessage.getEducationAttainment(), "6", 5)
                .end(5);
        data.setEducation(educationAttainment);
        // 社保
        Integer socialSecurity = ChooseEq.create(apiApplyMessage.getSocialSecurity())
                .when("1", 0)
                .when("2", 1)
                .when("3", 2)
                .when("4", 1)
                .end(0);
        data.setSocialSecurity(socialSecurity);
        // 逾期
        Integer overdueChoose = ChooseEq.create()
                .when(apiApplyMessage.getOverdue(), "1", 4)
                .when(apiApplyMessage.getOverdue(), "2", 2)
                .when(apiApplyMessage.getOverdue(), "3", 3)
                .end(4);
        data.setOverdue(overdueChoose);
        // 职业
        Integer careerChoose = ChooseEq.create()
                .when(apiApplyMessage.getCareer(), "1", 1)
                .when(apiApplyMessage.getCareer(), "2", 2)
                .when(apiApplyMessage.getCareer(), "3", 4)
                .when(apiApplyMessage.getCareer(), "4", 5)
                .when(apiApplyMessage.getCareer(), "5", 3)
                .end(1);
        data.setProfession(careerChoose);
        // 工资发放形式
        Integer salaryPayMethod = ChooseEq.create(apiApplyMessage.getSalaryPayMethod())
                .when("1", 3)
                .when("2", 1)
                .when("3", 2)
                .end(3);
        data.setPaymentForm(salaryPayMethod);
        // 工作时间
        Integer yearsService = ChooseEq.create()
                .when(apiApplyMessage.getYearsService(), "1", 1)
                .when(apiApplyMessage.getYearsService(), "2", 2)
                .when(apiApplyMessage.getYearsService(), "3", 3)
                .end(3);
        data.setWorkTime(yearsService);
        // 房产信息
        Integer realEstateInfo = ChooseEq.create(apiApplyMessage.getRealEstateInfo())
                .when("1", 1)
                .when("2", 2)
                .when("3", 2)
                .when("4", 2)
                .when("5", 2)
                .when("6", 2)
                .end(1);
        data.setHouse(realEstateInfo);
        // 公积金
        Integer providentFund = ChooseEq.create(apiApplyMessage.getProvidentFund())
                .when("1", 0)
                .when("2", 1)
                .when("3", 2)
                .when("4", 1)
                .end(0);
        data.setAccumulationFound(providentFund);
        // 贷款金额(元)
        Integer loanAmount = ChooseEq.create(apiApplyMessage.getLoanAmount())
                .when("1", 1)
                .when("2", 2)
                .when("3", 3)
                .when("4", 4)
                .end(2);
        data.setLoanAmount(loanAmount);
        // 车产信息
        Integer carInfo = ChooseEq.create(apiApplyMessage.getCarInfo())
                .when("1", 1)
                .when("2", 3)
                .when("3", 2)
                .when("4", 3)
                .end(1);
        data.setCar(carInfo);
        data.setCarAmount(0);
        data.setCarYear(0);
        data.setCarMortgageStatus(0);
        data.setIsCarOwner(0);
        //  借款期限
        Integer loanTerm= ChooseEq.create()
                .when(apiApplyMessage.getLoanTerm(), "1", 1)
                .when(apiApplyMessage.getLoanTerm(), "2", 2)
                .when(apiApplyMessage.getLoanTerm(), "3", 3)
                .when(apiApplyMessage.getLoanTerm(), "4", 4)
                .when(apiApplyMessage.getLoanTerm(), "5", 5)
                .when(apiApplyMessage.getLoanTerm(), "6", 5)
                .end(1);
        data.setLoanTime(loanTerm);
        // 保险保单
        Integer insurancePolicy = ChooseEq.create(apiApplyMessage.getInsurancePolicy())
                .when("1", 1)
                .when("2", 2)
                .when("3", 2)
                .when("4", 2)
                .end(1);
        data.setCommercialInsurance(insurancePolicy);
        // 信用卡（0-无；1-2万元以内；2-2万元以上
        Integer creditLimit = ChooseEq.create(apiApplyMessage.getCreditLimit())
                .when("1", 1)
                .when("2", 2)
                .when("3", 3)
                .when("4", 4)
                .when("5", 5)
                .when("6", 2)
                .end(1);
        data.setCreditCardLimit(creditLimit);
        data.setWeiLiDai(0);
        // 芝麻信用分
        Integer sesameCreditChoose = ChooseEq.create()
                .when(apiApplyMessage.getSesameCredit(), "1", 2)
                .when(apiApplyMessage.getSesameCredit(), "2", 3)
                .when(apiApplyMessage.getSesameCredit(), "3", 4)
                .when(apiApplyMessage.getSesameCredit(), "4", 5)
                .end(0);
        data.setZhiMa(sesameCreditChoose);
        //  花呗
        Integer huaBei = ChooseEq.create()
                .when(apiApplyMessage.getHbLimit(), "1", 0)
                .when(apiApplyMessage.getHbLimit(), "2", 1)
                .when(apiApplyMessage.getHbLimit(), "3", 3)
                .when(apiApplyMessage.getHbLimit(), "4", 3)
                .when(apiApplyMessage.getHbLimit(), "5", 3)
                .end(0);
        data.setHuaBei(huaBei);
        //  白条
        Integer btLimit = ChooseEq.create()
                .when(apiApplyMessage.getBtLimit(), "1", 0)
                .when(apiApplyMessage.getBtLimit(), "2", 1)
                .when(apiApplyMessage.getBtLimit(), "3", 3)
                .when(apiApplyMessage.getBtLimit(), "4", 3)
                .when(apiApplyMessage.getBtLimit(), "5", 3)
                .end(0);
        data.setBaiTiao(btLimit);
        String signStr = APP_KEY + APP_SECRET + apiApplyMessage.getPhoneMd5() + time;
        String s = DigestUtils.md5Hex(signStr);
        data.setSign(s);
        return JsonUtils.toJson(data);
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
